package com.sctlqq.wuye.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sctlqq.wuye.entity.RefuseClassification;
import com.sctlqq.wuye.entity.Result;
import com.sctlqq.wuye.entity.User;
import com.sctlqq.wuye.service.RefuseClassificationService;
import com.sctlqq.wuye.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sctlqq/api/wuye/refuseclassification")
public class RefuseClassificationController {

    @Autowired
    private RefuseClassificationService refuseClassificationService;

    @Autowired
    private UserService userService;

    /**
     * 获取所有的 垃圾分类打卡单
     * @return
     */
    @GetMapping("getrefuseclassificationlist")
    public Result<Object> getRefuseClassificationList(){
        return new Result<>(HttpStatus.OK.value(),"success",refuseClassificationService.list());
    }

    /**
     * 获取待审核（审核中）的垃圾分类打卡
     * @return
     */
    @GetMapping("getnewrefuseclassificationlist")
    public Result<Object> getNewRefuseClassificationList(){
        return new Result<>(HttpStatus.OK.value(),"success",refuseClassificationService.list(new QueryWrapper<RefuseClassification>().eq("status","审核中").orderByDesc("time")));
    }

    /**
     * 根据业主uid 获取该业主的垃圾分类打卡列表
     * @param uid 业主uid
     * @return
     */
    @PostMapping("/getrefuseclassificationlistbyuid")
    public Result<Object> getRefuseClassificationListByUid(@RequestBody Long uid){
        return new Result<>(HttpStatus.OK.value(),"success",refuseClassificationService.list(new QueryWrapper<RefuseClassification>().eq("uid",uid).orderByDesc("time")));
    }

    /**
     * 根据id 获取垃圾分类打卡 信息 包括业主信息   用于审核页面显示
     * @param id  垃圾分类 id
     * @return
     */
    @PostMapping("/getrefuseclassificationlistbyid")
    public Result<Object> getRefuseClassificationListById(@RequestBody Long id){
        //先根据打卡的id  获取打卡数据对象
        RefuseClassification refuseClassification= refuseClassificationService.getById(id);
        //再根据打卡数据对象中的uid，获取打卡的业主数据对象
        User user=userService.getById(refuseClassification.getUid());
        //将两个对象封装到map中
        List<Map> mapList=new ArrayList<>();
        Map<String, RefuseClassification> map=new HashMap<>();
        Map<String, User> map2=new HashMap<>();
        map.put("refuseClassification",refuseClassification);
        map2.put("ownerUser",user);
        //再将map加入到maplist 中
        mapList.add(map);
        mapList.add(map2);
        //返回maplist
        return new Result<>(HttpStatus.OK.value(),"success",mapList);
    }


    /**
     * 通过指定对象，新增 单个垃圾分类打卡请求单
     * @param refuseClassification  打卡信息对象
     * @return  true or false
     */
    @PostMapping("addrefuseclassification")
    public Result<Object> addRefuseClassification(@RequestBody RefuseClassification refuseClassification){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",refuseClassificationService.save(refuseClassification));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }


    /**
     * 更新垃圾分类打卡数据  垃圾分类打卡审核
     * @param refuseClassification
     * @return
     */
    @PutMapping("updaterefuseclassification")
    public Result<Object> updateRefuseClassification(@RequestBody RefuseClassification refuseClassification){
        Map<String, Boolean> map=new HashMap<>();
        map.put("isok",refuseClassificationService.updateById(refuseClassification));
        return new Result<>(HttpStatus.OK.value(),"success",map);
    }


}
